package cn.tedu.travelsystem.base.service;

import cn.tedu.travelsystem.hotel.pojo.entity.Hotel;
import cn.tedu.travelsystem.scenicSpot.pojo.entity.ScenicSpot;
import cn.tedu.travelsystem.transport.pojo.entity.Transport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class MonitoringService {

    private final AlertService alertService;

    public void checkHotelOverbook(Hotel hotel, int currentAvailableBeds) {
        if (currentAvailableBeds < 0) {
            String msg = String.format("%s 可用床位不足！当前剩余：%d，原库存：%d",
                    hotel.getHotel_name(), currentAvailableBeds, hotel.getCurrent_inventory());
            alertService.sendAlert("🏨 酒店库存超售预警", msg);
        }
    }

    public void checkScenicOverbook(ScenicSpot spot, int currentReservation) {
        if (currentReservation > spot.getMax_capacity()) {
            String msg = String.format("%s 当前预约人数：%d，超过最大容量：%d",
                    spot.getSpot_name(), currentReservation, spot.getMax_capacity());
            alertService.sendAlert("🏞️ 景区超载预警", msg);
        }
    }

    public void checkTransportOverbook(Transport transport, int currentAvailableSeats) {
        int seatLimit = getMaxSeatsForTransport(transport);
        if (currentAvailableSeats < 0) {
            String msg = String.format("%s → %s 交通工具已超售！当前余座：%d，总容量：%d",
                    transport.getDeparture(), transport.getDestination(), currentAvailableSeats, seatLimit);
            alertService.sendAlert("🚌 交通超售预警", msg);
        }
    }

    private int getMaxSeatsForTransport(Transport transport) {
        return switch (transport.getSeat_class()) {
            case "经济舱" -> 100;
            case "商务舱" -> 50;
            case "头等舱" -> 20;
            default -> 80;
        };
    }
}
